package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
@Slf4j
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    @PostMapping("/sub")
    public R delete(@RequestBody ShoppingCart shoppingCart){
        LambdaQueryWrapper<ShoppingCart> shoppingCartLqw = new LambdaQueryWrapper<>();
        shoppingCartLqw.eq(shoppingCart.getDishId() != null,ShoppingCart::getDishId,shoppingCart.getDishId());
        shoppingCartLqw.eq(shoppingCart.getSetmealId() != null,ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        ShoppingCart shoppingCartServiceOne = shoppingCartService.getOne(shoppingCartLqw);
        Integer number = shoppingCartServiceOne.getNumber();
        if (number == 1){
            shoppingCartService.remove(shoppingCartLqw);
            return R.success("删除成功");
        }else {
            shoppingCartServiceOne.setNumber(shoppingCartServiceOne.getNumber() - 1);
            shoppingCartService.updateById(shoppingCartServiceOne);
            return R.success("减少成功");
        }
    }

    @DeleteMapping("/clean")
    public R clean(){
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId,BaseContext.getId());
        shoppingCartService.remove(lqw);
        return R.success("清空购物车成功");
    }

    @PostMapping("/add")
    public R add(@RequestBody ShoppingCart shoppingCart) {
        log.info("接受到的参数{}",shoppingCart);
        shoppingCart.setUserId(BaseContext.getId());
        LambdaQueryWrapper<ShoppingCart> shoppingCartLqw = new LambdaQueryWrapper<>();
        shoppingCartLqw.eq(shoppingCart.getDishId() != null,ShoppingCart::getDishId,shoppingCart.getDishId());
        shoppingCartLqw.eq(shoppingCart.getSetmealId() != null,ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        ShoppingCart shoppingCartServiceOne = shoppingCartService.getOne(shoppingCartLqw);
        if (shoppingCartServiceOne == null){
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
            return R.success(shoppingCart);
        }else {
            shoppingCartServiceOne.setCreateTime(LocalDateTime.now());
            shoppingCartServiceOne.setNumber(shoppingCartServiceOne.getNumber() + 1);
            shoppingCartService.updateById(shoppingCartServiceOne);
            return R.success(shoppingCartServiceOne);
        }
    }

    @GetMapping("/list")
    public R get() {
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId,BaseContext.getId());
        lqw.orderByDesc(ShoppingCart::getCreateTime);
        List<ShoppingCart> list = shoppingCartService.list(lqw);
        return R.success(list);
    }
}
